package com.learn.aop;

import com.learn.aop.expression.FooService;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.AopUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;

/**
 * Pointcut 如何匹配到 join point
 * https://blog.csdn.net/wang489687009/article/details/120895007
 */
public class ExpressionMatchTest {

	public static void main(String[] args) {
		AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
//        pc.setExpression("execution(* com.learn.aop.expression.FooService.*(..))");
//        pc.setExpression("execution(void com.learn.aop.expression.*.*(..))");
		pc.setExpression("execution(* com.learn.aop.expression.*.*(int))");

		// 类级别的匹配
		boolean rlt = AopUtils.canApply(pc, FooService.class);
		System.out.println(pc.getExpression() + ", 匹配:" + FooService.class.getName() + ", 结果:" + rlt);

		System.out.println("--------------------");
		// 方法级别的匹配
		Method[] methods = FooService.class.getDeclaredMethods();
		for (Method method : methods) {
			boolean matches = pc.matches(method, method.getDeclaringClass());
			System.out.println(pc.getExpression() + ", 匹配:" + method + ", 结果:" + matches);
		}
	}
}
/* Output:
execution(* com.learn.aop.expression.*.*(int)), 匹配:com.learn.aop.expression.FooService, 结果:true
--------------------
execution(* com.learn.aop.expression.*.*(int)), 匹配:public java.lang.String com.learn.aop.expression.FooService.m2(int), 结果:true
execution(* com.learn.aop.expression.*.*(int)), 匹配:public void com.learn.aop.expression.FooService.doBiz(), 结果:false
execution(* com.learn.aop.expression.*.*(int)), 匹配:public java.lang.String com.learn.aop.expression.FooService.m1(), 结果:false
*///~